//创建用户相关的小仓库
import { defineStore } from 'pinia'
//引入接口
import { reqRegister, reqLogin } from '@/api/user/index.ts'
import type { RouteRecordRaw } from 'vue-router'
//引入路由（常量路由）
import { constantRoute, anyRoute, navRoute } from '@/router/routes'
import router from '@/router'
//引入深拷贝方法
import cloneDeep from 'lodash/cloneDeep'

//创建用户小仓库
const useUserStore = defineStore('User', {
    //小仓库存储数据
    state: (): UserState => {
        return {
            userId: -1,
            username: '请登录',
            avatar: 'https://gd-hbimg.huaban.com/689a639c005bddbad7bae709038b4fff36b8c9f2cdc8-PTyTst_fw236',
            realname: '',
            chatListFriends: GET_CHATLISTFRIENDS() as unknown as friend[],
            friends: GET_FRIENDS() as unknown as friend[],
            token: GET_TOKEN(), //获取用户唯一标识token
            changeAvatar: null, // 用户选择的即将更改的头像
        }
    },
    //异步、逻辑
    actions: {
        //用户注册的方法
        async userRegister(data: registerFormData) {
            const result: responseData = await reqRegister(data)
            //成功
            if (result.code == 200) {
                //保证当前async函数返回一个成功的promise
                return 'ok'
            } else {
                return Promise.reject(new Error(result.message))
            }
        },

        //用户登录的方法
        async userLogin(data: loginFormData) {
            const result: loginResponseData = await reqLogin(data)
            //成功
            if (result.code == 200) {
                //在pinia仓库储存一下token
                this.token = result.data.token
                //本地存储持久化存储一份
                SET_TOKEN(result.data.token)
                //保证当前async函数返回一个成功的promise
                return 'ok'
            } else {
                return Promise.reject(new Error(result.message))
            }
        },

        //获取用户信息的方法
        async userInfo() {
            const userInfo: userInfoResponseData = await reqUserInfo()
            const userFriends: userFriendsResponseData = await reqUserFriends()
            const chatListFriends: chatListFriendsResponseData = await reqChatListFriends()
            //如果获取用户信息成功，储存用户信息
            if (userInfo.code == 200) {
                this.userId = userInfo.data.userId
                this.username = userInfo.data.username
                this.avatar = userInfo.data.avatar
                this.realname = userInfo.data.realname
                this.chatListFriends = chatListFriends.data
                this.friends = userFriends.data
                return 'ok'
            } else {
                const error = userInfo.message + userFriends.message
                return Promise.reject(new Error(error))
            }
        },
        //退出登录的方法
        async userLogout() {
            const result: any = await reqLogout()
            if (result.code == 200) {
                this.userId = -1
                this.token = ''
                this.username = '请登录'
                this.avatar =
                    'https://gd-hbimg.huaban.com/689a639c005bddbad7bae709038b4fff36b8c9f2cdc8-PTyTst_fw236'
                this.realname = '用户未登录'
                this.chatListFriends = []
                this.friends = []
                REMOVE_TOKEN()
                REMOVE_CHATLISTFRIENDS()
                REMOVE_FRIENDS()
                REMOVE_CHAT()
                return 'ok'
            } else {
                return Promise.reject(new Error(result.message))
            }
        },
    },
    getters: {},
})

//对外暴露获取小仓库的方法
export default useUserStore
